$(document).ready(function () {
    getUserName()
    getCount()
    getInventory()
    getitemerror()
    getCreateItemWorning()                           
})

function getInventory() {
    axios.get('/Inventory/GetInventory', {})
        .then(function (result) {
            $("#content-block1").html(result.data);
        })
}

function getUserName() {
    axios.get('/Layout/GetUserName', {})
        .then(function (result) {
            document.getElementById("UserName").innerHTML = result.data;
        })
}

function getCount() {
    axios.get('/Layout/GetCount', {})
        .then(function (Count) {
            document.getElementById("InventoryCount").innerHTML = Count.data[0];
            document.getElementById("finishItemCount").innerHTML = Count.data[1];
            document.getElementById("OverdueItemCount").innerHTML = Count.data[2];
            document.getElementById("notFinishItemCount").innerHTML = Count.data[3];
        })
}

function viewCreateInventory() {
    axios.get('/Inventory/ViewCreateInventory', {})
        .then(function (result) {
            $("#content-block1").html(result.data);
        })
}
function showInventoryItems(inventoryId) {
    showNoFinishItems(inventoryId)
    showFinishItems(inventoryId)
    showOverdueItems(inventoryId)
    getCount()
}

function showNoFinishItems(inventoryId) {
    axios.get('/Item/ShowNoFinishItems', {
        params: { inventoryId: inventoryId}
    })
        .then(function (result) {
            $("#content-block2").html(result.data);
        })
}
function showFinishItems(inventoryId) {
    axios.get('/Item/ShowFinishItems', {
        params: { inventoryId: inventoryId }
    })
        .then(function (result) {
            $("#content-block3").html(result.data);
        })
}
function showOverdueItems(inventoryId) {
    axios.get('/Item/ShowOverdueItems', {
        params: { inventoryId: inventoryId }
    })
        .then(function (result) {
            $("#content-block4").html(result.data);
        })
}

function AfterViewCreateInventory() {
    var code = event.keyCode;
    if (code === 13) {
        inventoryName=$("#create-inventory-name").val();
        axios.post('/Inventory/CreateInventory', 
            $.param({
                inventory: inventoryName
            })
        )
            .then(function (result) {
                $("#content-block1").html(result.data);
                getCount();
            }).catch(function (error) {
                alert("错误");
                console.log(error)
            });
    }
}

function DeleteInventory() {
    var inventoryId = $("#input").val();
    axios.post('/Inventory/RemoveInventory',
        $.param({
            inventoryId: inventoryId
        })
    )
        .then(function (result) {
            $(".modal-backdrop").css("display", "none");
            $("#content-block1").html(result.data);
            getCount()
            showInventoryItems(inventoryId)
        })
}

function ChangeStatue(itemId,inventoryId) {
    axios.post('/Item/ChangeStatueInInventory',
        $.param({
            itemId: itemId 
        })
    )
        .then(function (data) {
            showNoFinishItems(inventoryId)
            showFinishItems(inventoryId)
            getCount()
        })
}

function deleteNofinishItem() {
    var itemId = $("#itemId").val(); 
    var inventoryId = $("#inventoryId").val();
    axios.post('/Item/DeleteItemInInventory',
        $.param({
            itemId: itemId,
            inventoryId: inventoryId
        })
        )
        .then(function (result) {
            $(".modal-backdrop").css("display", "none");
            showInventoryItems(result.data)
        })
        .catch(function (error) {
            alert("错误");
            console.log(error)
        });
}

function deleteInSearch() {
    var itemId = $("#itemId").val();
    axios.get('/Item/DeleteItemInSearch', {
        params: { itemId: itemId}
        })
        .then(function (result) {
            $(".modal-backdrop").css("display", "none");
            $("#findItem-delete").css("display", "none");
            $("#SearchResult-findItem").html(result.data);
        })
        .catch(function (error) {
            alert("错误");
            console.log(error)
        });
}

function getitemerror() {
    $("#item-content").blur(function () {
        var name = $("#item-content").val();
        if (name === "") {
            $("#content-error").html("内容不能为空!");
            return;
        }
        else if (name !== "") {
            var a = $("#content-error").text();
            $("#content-error").html("")
            return;
        }
    })
    $("#item-content").keypress(function () {
        var a = $("#content-error").text();
        $("#content-error").html("请输入50字以内。");
        return;
    })
    $("#item-mark").blur(function () {
        var name = $("#item-mark").val();
        if (name === "") {
            $("#mark-error").html("内容不能为空!");
            return;
        }
        else if (name !== "") {
            var a = $("#mark-error").text();
            $("#mark-error").html("")
            return;
        }
    })
    $("#item-mark").keypress(function () {
        var a = $("#content-error").text();
        $("#mark-error").html("请输入50字以内。");
        return;
    })

}

function getCreateItemWorning() {
    $("#item-create-content").blur(function () {
        var name = $("#item-create-content").val();
        if (name === "") {
            $("#create-content-warning").html("内容不能为空!");
            return;
        }
        else if (name !== "") {
            $("#create-content-warning").html("")
            return;
        }
    })
    $("#item-create-content").keypress(function () {
        $("#create-content-warning").html("请输入50字以内。");
        return;
    })
    $("#item-create-mark").blur(function () {
        var name = $("#item-create-mark").val();
        if (name === "") {
            $("#create-mark-warning").html("内容不能为空!");
            return;
        }
        else if (name !== "") {

            $("#create-mark-warning").html("")
            return;
        }
    })
    $("#item-create-mark").keypress(function () {
        var a = $("#create-mark-warning").text();
        $("#create-mark-warning").html("请输入50字以内。");
        return;
    })
}